package com.hsy.leancloudchat.main

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import cn.leancloud.LCUser
import cn.leancloud.im.v2.LCIMConversation
import com.hsy.leancloudchat.LeanCloudHelper
import com.hsy.leancloudchat.chat.ChatActivity
import com.hsy.leancloudchat.databinding.ItemConversationExistBinding

class ConversationExistAdapter(lifecycleOwner: LifecycleOwner) :
    RecyclerView.Adapter<ConversationExistAdapter.ConversationExistViewHolder>() {
    private val mLifecycleOwner = lifecycleOwner
    var conversationExistList = mutableListOf<LCIMConversation>()
        @SuppressLint("NotifyDataSetChanged")
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ConversationExistViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return ConversationExistViewHolder(
            ItemConversationExistBinding.inflate(
                layoutInflater,
                parent,
                false
            )
        )
    }

    override fun getItemCount(): Int {
        return conversationExistList.size
    }

    override fun onBindViewHolder(holder: ConversationExistViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class ConversationExistViewHolder(private val binding: ItemConversationExistBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(position: Int) {
            val mConversation = conversationExistList[position]
            binding.apply {
                val foundUserLiveData = MutableLiveData<LCUser?>()
                val friendId = if (mConversation.members[0] == LCUser.currentUser().objectId) {
                    mConversation.members[1]
                } else {
                    mConversation.members[0]
                }
                LeanCloudHelper.getInstance().getUserInBackGround(
                    friendId,
                    foundUserLiveData
                )
                foundUserLiveData.observe(mLifecycleOwner) {
                    tvName.text = it?.username
                }

                tvLastMessage.text = mConversation.lastMessage?.content?.let {
                    LeanCloudHelper.getInstance()
                        .parseMessageContentForText(it)
                }
                tvTime.text = mConversation.lastMessage?.timestamp?.let {
                    LeanCloudHelper.getInstance()
                        .parseTimeStamp(it)
                }
            }

            binding.root.setOnClickListener {
                val mIntent = Intent(it.context, ChatActivity::class.java).apply {
                    putExtra("conversationId", mConversation.conversationId)
                    putExtra("friendUserName", binding.tvName.text.toString())
                }
                it.context.startActivity(mIntent)
            }
        }
    }
}